package com.herench.bs.handler;


import com.herench.bs.agent.IOutpatientClinicAgent;
import com.herench.bs.annotation.Handler;
import com.herench.bs.support.AbstractMessageHandler;
import com.herench.bs.support.RequestMessage;
import com.herench.bs.support.ResponseMessage;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by ThinkPad on 2017/8/29.
 */

@Component
@Handler(code = "100505",name="科室查询")
public class HospitalDeptsHandler extends AbstractMessageHandler {

    @Override
    protected ResponseMessage innerHandler(RequestMessage requestMessage) throws Exception {
        ResponseMessage responseMessage = new ResponseMessage(requestMessage.getId());
        String channelCode = requestMessage.getArgsPropertyStringValue("sysCode");
        Assert.notNull(channelCode, "sysCode 不能为空");
        String hosId = requestMessage.getArgsPropertyStringValue("hosId");
        String state = requestMessage.getArgsPropertyStringValue("resType");
        Assert.notNull(state, "resType 不能为空");
        IOutpatientClinicAgent outpatientClinicAgent = getCleverClinicContext().getApplicationContext().getBean(IOutpatientClinicAgent.class);
        Map<String, Object> params = new HashMap<>();
        params.put("channelCode", channelCode);
        params.put("alive", true);
        params.put("state", state);
        String  hospitalCode= getCleverClinicContext().getHospitalCodeByChannelCode(channelCode);
        params.put("hospitalCode", hospitalCode);
        if (!StringUtils.hasLength(hosId)){
            hosId = getCleverClinicContext().getHospitalCodeByChannelCode(channelCode);
        }
        Map<String, Object> res = outpatientClinicAgent.loadVisitDepts(channelCode,hosId,state);
        getPropertiesFromInvokerResultMap(responseMessage, res);
        if (responseMessage.getCode().equals("0")) {
            responseMessage.setDataAttribute("dataList", res.get("depts"));
        }

        return responseMessage;
    }
}
